//
//  LCHomeCollectionViewCell.m
//  段子手
//
//  Created by dllo on 16/3/17.
//  Copyright © 2016年 JokesTeam. All rights reserved.
//

#import "LCHomeCollectionViewCell.h"


@interface LCHomeCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableView;

@property (nonatomic, assign)CGFloat heightForCommentTableView;

@property (nonatomic, assign) CGRect rectPassage;

@property (nonatomic, retain) NSMutableArray *arrModel;

@property (nonatomic, assign) BOOL isUpState;


@end


@implementation LCHomeCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self create];
    }
    return self;
}


//- (void)setArrModel:(NSMutableArray *)arrModel{
//    _arrModel = arrModel;
//    [_tableView reloadData];
//    
//}


- (void)setStrForGet:(NSString *)StrForGet{
    
    _StrForGet = StrForGet;
        [self FirstPageDataHandle];
}



- (void)create {
    
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.contentView.bounds.size.width, self.contentView.bounds.size.height) style:UITableViewStylePlain];
    
        _tableView.delegate = self;
        _tableView.dataSource = self;
    
        [_tableView registerClass:[LCHomePassageTableViewCell class] forCellReuseIdentifier:@"passageCell"];
    
       [_tableView registerClass:[LCHomeVedioTableViewCell class] forCellReuseIdentifier:@"vedioTableViewCell"];
    
    [_tableView registerClass:[LCHomeImageTableViewCell class] forCellReuseIdentifier:@"imageCell"];
    
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

//    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.contentView addSubview:_tableView];
    
    self.heightForCommentTableView = 0;
    
//    [self addHeader];
//    [self addFooter];
    
    
}



/**< 下拉刷新 */
- (void)addHeader{
    
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        // 标记为下拉
        self.isUpState = NO;
        
        
        [self FirstPageDataHandle];
        
    }];
    [_tableView.mj_header beginRefreshing];
    
}
/**< 上拉加载 */
- (void)addFooter{
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        // 标记为上拉
        _isUpState = YES;
        
        [self FirstPageDataHandle];
        
    }];
    
    [_tableView.mj_footer beginRefreshing];
}



#pragma mark - tableView的协议方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LCHomeTableViewPassageModel *model = _arrModel[indexPath.row];
    
    if ([[NSString stringWithFormat:@"%@",  model.media_type] isEqualToString:@"0"]) {
        LCHomePassageTableViewCell *passageCell = [tableView dequeueReusableCellWithIdentifier:@"passageCell"];
        passageCell.model = model;
      
            return passageCell;
        
        
    }else if ([[NSString stringWithFormat:@"%@",  model.media_type] isEqualToString:@"3"]){
        LCHomeVedioTableViewCell *vedioTableViewCell = [tableView dequeueReusableCellWithIdentifier:@"vedioTableViewCell"];
        vedioTableViewCell.model = model;
        
        return vedioTableViewCell;
        
        
        
    }else if([[NSString stringWithFormat:@"%@",  model.media_type] isEqualToString:@"1"]||[[NSString stringWithFormat:@"%@",  model.media_type] isEqualToString:@"2"]){
        LCHomeImageTableViewCell *imageCell = [tableView dequeueReusableCellWithIdentifier:@"imageCell"];
    
        imageCell.model = model;
        
        return imageCell;
    
    }else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        cell.hidden = YES;
        return cell;
    }
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
            for (UIView* next = [self superview]; next; next = next.superview) {
                UIResponder* nextResponder = [next nextResponder];
                if ([nextResponder isKindOfClass:[UIViewController class]]) {
                  UIViewController *vv = (UIViewController*)nextResponder;
                    
                    
                    LCBaseTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
                    
                    LCDetialViewController *LCDVC = [[LCDetialViewController alloc]init];
                    
                    LCHomeTableViewPassageModel *model = _arrModel[indexPath.row];
                    
                    LCDVC.model = model;
                    
                    LCDVC.CellH = cell.frame.size.height;
                    
//                    NSLog(@" LCDVC.CellH====%f",  LCDVC.CellH)
//                    
//                    NSLog(@"qqqqqqqqqqqqqqqqq~~~~~%@", model.CellStr);
                    
                    [vv.navigationController pushViewController:LCDVC animated:YES];
                    
                }
            }

    
    
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _arrModel.count;
}





- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LCHomeTableViewPassageModel *model = _arrModel[indexPath.row];
    
    if ([[NSString stringWithFormat:@"%@",  model.media_type]  isEqualToString:@"0"]) {

    
//    LCHomeTableViewPassageModel *model = _arrModel[indexPath.row];
    
    
    self.rectPassage = [WxlAPPTools scripeSuitWidthWithString:model.text Font:14 Size:CGSizeMake(SCREENW - 40, 0)];
        
        CGFloat h = 0;
        
        if (model.comments.count != 0) {
            
     for (int i = 0; i < model.comments.count; i++) {
         
           NSDictionary *arr  = [NSDictionary dictionaryWithDictionary:model.comments[i]];
         
         NSString *str = arr[@"text"];
                CGRect rect = [WxlAPPTools scripeSuitWidthWithString:str Font:13 Size:CGSizeMake(SCREENW - 20 - 60, 0)];
                h += rect.size.height + 50;
            }
        }
    
        return h + _rectPassage.size.height + 150;
        
        
        
        
    }else if ([[NSString stringWithFormat:@"%@",  model.media_type]  isEqualToString:@"3"]){
        
        self.rectPassage = [WxlAPPTools scripeSuitWidthWithString:model.text Font:14 Size:CGSizeMake(SCREENW - 40, 0)];
        
        CGFloat h = 0;
        
        if (model.comments.count != 0) {
            
            for (int i = 0; i < model.comments.count; i++) {
                
                NSDictionary *arr  = [NSDictionary dictionaryWithDictionary:model.comments[i]];
                
                NSString *str = arr[@"text"];
                CGRect rect = [WxlAPPTools scripeSuitWidthWithString:str Font:13 Size:CGSizeMake(SCREENW - 20 - 60, 0)];
                h += rect.size.height + 50;
            }
        }
        
       CGFloat vedioH = (SCREENW - 40) * [model.origin_video[@"height"] floatValue] / [model.origin_video[@"width"] floatValue];
        
        
        
        return h + _rectPassage.size.height + vedioH + 160;
        
        
    }else if([[NSString stringWithFormat:@"%@",  model.media_type] isEqualToString:@"1"]||[[NSString stringWithFormat:@"%@",  model.media_type] isEqualToString:@"2"]){

        self.rectPassage = [WxlAPPTools scripeSuitWidthWithString:model.text Font:14 Size:CGSizeMake(SCREENW - 40, 0)];
        
        CGFloat h = 0;
        
        if (model.comments.count != 0) {
            
            for (int i = 0; i < model.comments.count; i++) {
                
                NSDictionary *arr  = [NSDictionary dictionaryWithDictionary:model.comments[i]];
                
                NSString *str = arr[@"text"];
                CGRect rect = [WxlAPPTools scripeSuitWidthWithString:str Font:13 Size:CGSizeMake(SCREENW - 20 - 60, 0)];
                h += rect.size.height + 50;
            }
        }

        CGFloat imgVH = (SCREENW - 40) * [model.large_image[@"height"] floatValue] / [model.large_image[@"width"] floatValue];

        
          return h + _rectPassage.size.height + imgVH + 160;
    }else {
        return 0;
    }
}



- (void)FirstPageDataHandle{

    
    [WxlAPPTools GETWithURL:_StrForGet par:nil success:^(id responseObject) {
        
        
       

        
        
        //下拉刷新
        if (self.isUpState == NO) {
            // 将数据存入ModelArr中
            
     self.arrModel = [LCHomeTableViewPassageModel modelHandelWithArray:responseObject[@"data"][@"data"]];
        }else{
            
            for (NSDictionary *dic in responseObject[@"data"][@"data"]) {
                LCHomeTableViewPassageModel *model = [[LCHomeTableViewPassageModel alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                
                [self.arrModel addObject:model];
            }
            
        }
        
        
        [_tableView reloadData];
        [_tableView.mj_footer endRefreshing];
        [_tableView.mj_header endRefreshing];
        
        
        
    } filed:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    
}



@end
